package com.bdqn.service;

import com.bdqn.pojo.SysUser;
import com.bdqn.dao.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Scope("prototype")
@Transactional
@Service("sysUserService") // 注册为一个Service层的Bean
public class SysUserServiceImpl implements SysUserService {
    @Autowired // @Resource
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        try {
            return sysUserMapper.selectSysUserList(sysUser);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 保存用户
     *
     * @param sysUser
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean add(SysUser sysUser) {
        // 模拟在一个事务中两次对数据库的操作，第一次操作完成之后发生异常时，第二次未操作未执行，第一次操作也将回滚。两次操作要么都不成功，要么同时成功
        sysUserMapper.add(sysUser);

        /*if (sysUser != null) {
            // 手动抛出异常
            throw new SendEMailException("发送通知邮件异常");
        }*/
        sysUserMapper.add(sysUser);
        return true;
    }

    public SysUserMapper getSysUserMapper() {
        return sysUserMapper;
    }

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

}
